#include "ui_weather.h"
#include "ntp_time.h"


static lv_obj_t *return_btn;
static lv_obj_t *hour;
static lv_obj_t *min;
static lv_obj_t *sec;
static lv_obj_t *weather_1;
static lv_obj_t *weather_2;
static lv_obj_t *year;


static lv_style_t icon_style;
static lv_timer_t *timer;

static lv_obj_t *hour_label;
static lv_obj_t *min_label;
static lv_obj_t *sec_label;
static lv_obj_t *weather_1_labal;
static lv_obj_t *weather_2_labal;
static lv_obj_t *year_labal;



static void timer_cb(lv_timer_t *timer);

static void ui_weather_check_page_anim_in(uint32_t delay)//进场动画
{
    obj_set_anim(return_btn, -320, 0, 500, delay, anim_set_y_cb, NULL, lv_anim_path_bounce);
    obj_set_anim(hour, -172, 20, 500, delay, anim_set_x_cb, NULL, lv_anim_path_bounce);
    obj_set_anim(min, -172, 70, 500, delay, anim_set_x_cb, NULL, lv_anim_path_bounce);   
    obj_set_anim(sec, -172, 120, 500, delay, anim_set_x_cb, NULL, lv_anim_path_bounce);   
    obj_set_anim(weather_1, -172, 20, 500, delay, anim_set_x_cb, NULL, lv_anim_path_bounce);   
    obj_set_anim(weather_2, -172, 20, 500, delay, anim_set_x_cb, NULL, lv_anim_path_bounce);   
    obj_set_anim(year, -172, 20, 500, delay, anim_set_x_cb, NULL, lv_anim_path_bounce);   
}

static void ui_weather_check_page_anim_out(uint32_t delay)//出场动画
{
    obj_set_anim(return_btn, 0, 172, 500, delay, anim_set_x_cb, lv_obj_del_anim_ready_cb, lv_anim_path_ease_out);
    obj_set_anim(hour, 30, -100, 500, delay, anim_set_y_cb, lv_obj_del_anim_ready_cb, lv_anim_path_bounce);
    obj_set_anim(min, 30, -100, 500, delay, anim_set_y_cb, lv_obj_del_anim_ready_cb, lv_anim_path_bounce);   
    obj_set_anim(sec, 30, -100, 500, delay, anim_set_y_cb, lv_obj_del_anim_ready_cb, lv_anim_path_bounce);  
    obj_set_anim(weather_1, 20, 330, 500, delay, anim_set_x_cb, lv_obj_del_anim_ready_cb, lv_anim_path_bounce);   
    obj_set_anim(weather_2, 20, 330, 500, delay, anim_set_x_cb, lv_obj_del_anim_ready_cb, lv_anim_path_bounce);   
    obj_set_anim(year, 20, 330, 500, delay, anim_set_x_cb, lv_obj_del_anim_ready_cb, lv_anim_path_bounce); 
}

static void draw_time(struct tm* time)
{
    lv_label_set_text_fmt(hour_label, "%02d:", time->tm_hour);
    lv_label_set_text_fmt(min_label, "%02d:", time->tm_min);
    lv_label_set_text_fmt(sec_label, "%02d", time->tm_sec);
    lv_label_set_text_fmt(year_labal, LV_SYMBOL_HOME"%04d.%02d.%02d", time->tm_year+1900,time->tm_mon+1,time->tm_mday);
    lv_label_set_text_fmt(weather_1_labal, LV_SYMBOL_EYE_OPEN"High temperature:%02d°C  ", weather.degree_high[0]);
    lv_label_set_text_fmt(weather_2_labal, LV_SYMBOL_CHARGE"Low temperature:%02d°C  ", weather.degree_low[0]);
}

/* timer cb */
static void timer_cb(lv_timer_t *timer)//时间回调
{
    draw_time(get_time());
}

static void return_btn_cb(lv_event_t *e)//按钮回调
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *label = lv_event_get_user_data(e);
    
    switch(code)
    {
        case LV_EVENT_FOCUSED:
            lv_style_set_text_opa(&icon_style, LV_OPA_COVER);
            lv_obj_add_style(label, &icon_style, LV_STATE_FOCUSED);
            break;
        case LV_EVENT_DEFOCUSED:
            lv_style_set_text_opa(&icon_style, LV_OPA_30);
            lv_obj_add_style(label, &icon_style, LV_EVENT_DEFOCUSED);
            break;
        case LV_EVENT_CLICKED:
            lv_group_remove_obj(return_btn);
            lv_timer_del(timer);
            ui_weather_check_page_anim_out(0);
            main_menu_page(0);
            break;
        default:
            break;
    }
}

void ui_weather_check_page(uint32_t delay)
{
    lv_group_t *group = get_group();

    /* retrun btn */
    //返回按钮与wifi的类似
    return_btn = lv_btn_create(bg_cont_obj);
    lv_obj_remove_style_all(return_btn);
    lv_obj_set_size(return_btn, 30, 30);
    lv_obj_set_style_radius(return_btn, 15, 0);
    lv_obj_set_style_bg_color(return_btn, lv_color_white(), LV_STATE_PRESSED);
    lv_obj_set_style_bg_opa(return_btn, LV_OPA_30, LV_STATE_PRESSED);
    lv_obj_align(return_btn, LV_ALIGN_TOP_LEFT, 5, 10);

    lv_style_init(&icon_style);
    lv_style_set_text_color(&icon_style, lv_color_white());
    lv_style_set_text_opa(&icon_style, LV_OPA_50);
    lv_style_set_text_font(&icon_style, &lv_font_montserrat_20);

    lv_obj_t *return_icon_label = lv_label_create(return_btn);
    lv_label_set_text(return_icon_label, LV_SYMBOL_LEFT);
    lv_obj_add_style(return_icon_label, &icon_style, 0);
    lv_obj_add_event_cb(return_btn, return_btn_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(return_btn, return_btn_cb, LV_EVENT_FOCUSED, return_icon_label);
    lv_obj_add_event_cb(return_btn, return_btn_cb, LV_EVENT_DEFOCUSED, return_icon_label);
    lv_obj_align(return_icon_label, LV_ALIGN_CENTER, 0, 0);

    /*温度*/
    weather_1 = lv_obj_create(bg_cont_obj);
    lv_obj_remove_style_all(weather_1);
    lv_obj_set_size(weather_1, 150, 80);
    lv_obj_set_pos(weather_1, 20, 125);
    lv_obj_set_style_bg_color(weather_1, lv_color_black(), 0); //设置为黑色的颜色
    lv_obj_set_style_bg_opa(weather_1, LV_OPA_60, 0);//背景透明度

    weather_1_labal = lv_label_create(weather_1);
    lv_obj_set_size(weather_1_labal, 120, 80);   
    lv_label_set_text_fmt(weather_1_labal, LV_SYMBOL_EYE_OPEN"High temperature:25°C  "); 
    lv_obj_set_style_text_color(weather_1_labal,lv_color_hex(MY_COLOR_RED), 0);
    lv_obj_set_style_text_font(weather_1_labal, &lv_font_montserrat_32, 0);
    lv_obj_set_style_text_opa(weather_1_labal, LV_OPA_COVER, 0);
    lv_label_set_long_mode(weather_1_labal,LV_LABEL_LONG_SCROLL_CIRCULAR);

    weather_2 = lv_obj_create(bg_cont_obj);
    lv_obj_remove_style_all(weather_2);
    lv_obj_set_size(weather_2, 150, 80);
    lv_obj_set_pos(weather_2, 20, 200);
    lv_obj_set_style_bg_color(weather_2, lv_color_black(), 0); //设置为黑色的颜色
    lv_obj_set_style_bg_opa(weather_2, LV_OPA_60, 0);//背景透明度

    weather_2_labal = lv_label_create(weather_2);
    lv_obj_set_size(weather_2_labal, 120, 80);   
    lv_label_set_text_fmt(weather_2_labal, LV_SYMBOL_CHARGE"Low temperature:10°C  "); 
    lv_obj_set_style_text_color(weather_2_labal,lv_color_hex(MY_COLOR_DODGER_BLUE), 0);
    lv_obj_set_style_text_font(weather_2_labal, &lv_font_montserrat_32, 0);
    lv_obj_set_style_text_opa(weather_2_labal, LV_OPA_COVER, 0);
    lv_label_set_long_mode(weather_2_labal,LV_LABEL_LONG_SCROLL_CIRCULAR);

    /*年月日*/
    year = lv_obj_create(bg_cont_obj);
    lv_obj_remove_style_all(year);
    lv_obj_set_size(year, 150, 80);
    lv_obj_set_pos(year, 20, 260);
    lv_obj_set_style_bg_color(year, lv_color_black(), 0); //设置为黑色的颜色
    lv_obj_set_style_bg_opa(year, LV_OPA_60, 0);//背景透明度

    year_labal = lv_label_create(year);
    lv_obj_set_size(year_labal, 150, 40);   
    lv_label_set_text_fmt(year_labal, LV_SYMBOL_HOME"2022.10.1"); 
    lv_obj_set_style_text_color(year_labal,lv_color_hex(MY_COLOR_PURPLE), 0);
    lv_obj_set_style_text_font(year_labal, &lv_font_montserrat_20, 0);
    lv_obj_set_style_text_opa(year_labal, LV_OPA_COVER, 0);
    lv_obj_center(year_labal);

    /*时*/
    hour = lv_obj_create(bg_cont_obj);
    lv_obj_remove_style_all(hour);
    lv_obj_set_size(hour, 50, 50);
    lv_obj_set_pos(hour, 20, 30);
    lv_obj_set_style_bg_color(hour, lv_color_black(), 0); //设置为黑色的颜色
    lv_obj_set_style_bg_opa(hour, LV_OPA_60, 0);//背景透明度

    hour_label = lv_label_create(hour);
    lv_label_set_text_fmt(hour_label, "00:");
    lv_obj_set_style_text_color(hour_label,lv_color_hex(MY_COLOR_DARK_ORANGE), 0);
    lv_obj_set_style_text_font(hour_label, &lv_font_montserrat_32, 0);
    lv_obj_set_style_text_opa(hour_label, LV_OPA_COVER, 0);
    lv_obj_center(hour_label);

    /*分*/
    min = lv_obj_create(bg_cont_obj);
    lv_obj_remove_style_all(min);
    lv_obj_set_size(min, 50, 50);
    lv_obj_set_pos(min, 70, 30);
    lv_obj_set_style_bg_color(min, lv_color_black(), 0); //设置为黑色的颜色
    lv_obj_set_style_bg_opa(min, LV_OPA_60, 0);//背景透明度

    min_label = lv_label_create(min);
    lv_label_set_text_fmt(min_label, "00:");
    lv_obj_set_style_text_color(min_label,lv_color_hex(MY_COLOR_DARK_ORANGE), 0);
    lv_obj_set_style_text_font(min_label, &lv_font_montserrat_30, 0);
    lv_obj_set_style_text_opa(min_label, LV_OPA_COVER, 0);
    lv_obj_center(min_label);   

    /*秒*/
    sec = lv_obj_create(bg_cont_obj);
    lv_obj_remove_style_all(sec);
    lv_obj_set_size(sec, 50, 50);
    lv_obj_set_pos(sec, 120, 30);
    lv_obj_set_style_bg_color(sec, lv_color_black(), 0); //设置为黑色的颜色
    lv_obj_set_style_bg_opa(sec, LV_OPA_60, 0);//背景透明度

    sec_label = lv_label_create(sec);
    lv_label_set_text_fmt(sec_label, "00");
    lv_obj_set_style_text_color(sec_label, lv_color_hex(MY_COLOR_DARK_ORANGE), 0);
    lv_obj_set_style_text_font(sec_label, &lv_font_montserrat_20, 0);
    lv_obj_set_style_text_opa(sec_label, LV_OPA_COVER, 0);
    lv_obj_align(sec_label, LV_ALIGN_CENTER, -5, 3);



    lv_group_add_obj(group, return_btn);//添加group
    timer = lv_timer_create(timer_cb, 200, NULL);//时间回调800ms
    ui_weather_check_page_anim_in(delay);
}

